﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Yappi.HierarchicalField
{
    public partial class HierarchicalFieldEditControl : UserControl, IFieldEditor
    {

        protected CheckBox JavaScriptPlacer;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupWebUrl;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupListName;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupFieldFullName;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupFieldShortName;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupFieldFilter;
        [CLSCompliant(false)]
        protected InputFormTextBox LookupFieldOrder;
        [CLSCompliant(false)]
        protected InputFormTextBox FilterValue;
        [CLSCompliant(false)]
        protected InputFormTextBox FilterWebUrl;
        [CLSCompliant(false)]
        protected InputFormTextBox FilterListName;
        [CLSCompliant(false)]
        protected InputFormTextBox FilterValuesFieldName;
        [CLSCompliant(false)]
        protected InputFormTextBox FilterValuesRowNumber;
        [CLSCompliant(false)]
        public void InitializeWithField(SPField field)
        {

            if (field == null)
                return;
            HierarchicalField lookupField = field as HierarchicalField;
            if (!this.IsPostBack)
            {
                LookupWebUrl.Text = lookupField.LookupWebUrl;
                LookupListName.Text = lookupField.LookupListName;
                LookupFieldFullName.Text = lookupField.LookupFieldFullName;
                LookupFieldShortName.Text = lookupField.LookupFieldShortName;
                LookupFieldFilter.Text = lookupField.LookupFieldFilter;
                LookupFieldOrder.Text = lookupField.LookupFieldOrder;
                FilterValue.Text = lookupField.FilterValue;
                FilterWebUrl.Text = lookupField.FilterWebUrl;
                FilterListName.Text = lookupField.FilterListName;
                FilterValuesFieldName.Text = lookupField.FilterValuesFieldName;
                FilterValuesRowNumber.Text = lookupField.FilterValuesRowNumber;
                JavaScriptPlacer.Checked = false;
                try
                {
                    JavaScriptPlacer.Checked = bool.Parse(lookupField.JavaScriptPlacer);
                }
                catch (FormatException) { }
            }
        }
        public string GetProperty(object name)
        {
            if (name == null)
                return string.Empty;
            else
                return name.ToString();
        }
        [CLSCompliant(false)]
        public void OnSaveChange(SPField field, bool isNewField)
        {
            HierarchicalField l = field as HierarchicalField;
            l.LookupWebUrl = LookupWebUrl.Text;
            l.LookupListName = LookupListName.Text;
            l.LookupFieldFullName = LookupFieldFullName.Text;
            l.LookupFieldShortName = LookupFieldShortName.Text;
            l.LookupFieldFilter = LookupFieldFilter.Text;
            l.LookupFieldOrder = LookupFieldOrder.Text;
            l.FilterValue = FilterValue.Text;
            l.FilterWebUrl = FilterWebUrl.Text;
            l.FilterListName = FilterListName.Text;
            l.FilterValuesFieldName = FilterValuesFieldName.Text;
            l.FilterValuesRowNumber = FilterValuesRowNumber.Text;
            l.JavaScriptPlacer = JavaScriptPlacer.Checked.ToString();
            //l.Update(true);
            // l.ParentList.Update();
        }



        public bool DisplayAsNewSection
        {
            get { return true; }
        }

    }
}
